<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../js/vue.js"></script>
</head>

<body>
  <div id="app">
    a:<input type="text" v-model.number="a"><br>
    b:<input type="text" v-model.number="b"><br>
    <output>{{total}}</output>
    <button @click="btnClickHandler">修改obj</button>
  </div>
  <script>
    let vm = new Vue({
      el: "#app",
      data: {
        a: null,
        b: null,
        total: 0,
        obj: {
          username: 'admin1'
        }
      },
      watch: {
        a(newVal, oldVal) {
          this.total = this.a + this.b
        },
        b(newVal, oldVal) {
          this.total = this.a + this.b
        },
        // obj() {
        //   // 普通监听形式，只能监听到引用数据类型引用地址的改变
        //   console.log('watch', this.obj);
        // },
        obj: {
          handler(newVal, oldVal) {
            console.log(newVal);
            console.log(oldVal);
          },
          deep: true
        }
      },
      methods: {
        btnClickHandler() {
          this.obj = {
            username: 'zhangsan'
          }
          // this.obj.username = 'zhangsan'
          // console.log(this.obj);
          // this.obj = {}
        }
      },
      created() {

      }
    })
  </script>
</body>

</html>